﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DAL;
namespace CafeTek
{
    public partial class FrmListProduct : Form
    {
        public ProductType GproductType = null; //then choose to treeview
        public Product Gproduct = null; //
        public ProductDB GproductDb = null;
        public ProductDB gproductdb = new ProductDB();

        public FrmListProduct()
        {
            InitializeComponent();
        }

        /// <summary>
        /// create child nodes
        /// </summary>
        /// <param name="Node"> nodes parent</param>
        /// <param name="item"> products</param>
        public void AddToTreeView(TreeNode Node, ProductType item)
        {
            IList<ProductType> listProductType = new List<ProductType>();
            ProductTypeDB productTypeDb = new ProductTypeDB();

            //get all product is child of item
            listProductType = productTypeDb.GetAll(item);
            foreach (ProductType _item in listProductType)
            {
                TreeNode _node = new TreeNode(_item.Name);
                _node.Tag = _item;
                Node.Nodes.Add(_node);
                //add its child nodes in treeview
                AddToTreeView(_node, _item);//recursive
            }
        }

        /// <summary>
        /// Load products on treeview
        /// </summary>
        public void LoadTreeView()
        {
            IList<ProductType> listProductType = new List<ProductType>();
            ProductTypeDB productTypeDb = new ProductTypeDB();

            //get all productType is parent
            listProductType = productTypeDb.GetAll(null);
            foreach (ProductType item in listProductType)
            {
                TreeNode node = new TreeNode(item.Name);
                node.Tag = item;
                AddToTreeView(node, item);
                treevProducttype.Nodes.Add(node);
            }
        }

        public void LoadProductToList(ProductType itemselect)
        {
            int autonumber = 0;
            lstvProduct.Items.Clear();
            IList<Product> list = new List<Product>();
            list = gproductdb.GetAll(itemselect.TypeId);
            foreach (Product item in list)
            {
                ListViewItem _itemlistview = new ListViewItem((++autonumber).ToString());
                _itemlistview.SubItems.Add(item.Name);
                _itemlistview.SubItems.Add(item.Price.ToString());
                _itemlistview.Tag = item;
                if (item.Visible == false)
                    _itemlistview.SubItems.Add("None");
                else
                    _itemlistview.SubItems.Add("Show");
                lstvProduct.Items.Add(_itemlistview);
            }
        }

        /// <summary>
        /// Load all Unit of product
        /// </summary>
        public void loadUnit()
        {
            UnitDB unitDB = new UnitDB();
            this.cmbunit.DataSource = unitDB.GetAll();
        }

        private void FrmProductType_Load(object sender, EventArgs e)
        {
            LoadTreeView();
            loadUnit();
        }

        //when choose a product
        private void treevProducttype_AfterSelect(object sender, TreeViewEventArgs e)
        {
            GproductDb = new ProductDB();
            GproductType = (ProductType)(treevProducttype.SelectedNode.Tag);
            LoadProductToList((ProductType)(treevProducttype.SelectedNode.Tag));
        }

        private void lstvProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstvProduct.SelectedItems.Count == 0)
                return;
            Gproduct = (Product)(lstvProduct.SelectedItems[0].Tag);
            txtname.Text = Gproduct.Name;
            txtPrice.Text = Gproduct.Price.ToString();
            chkVisible.Checked = Gproduct.Visible;
            this.cmbunit.SelectedItem = Gproduct.Unit;

        }

        /// <summary>
        /// check have not correct format name and price
        /// </summary>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        public string checkValue(string name, string price)
        {
            if (String.IsNullOrEmpty(name.Trim()))
                return "Please input the product name";
            if (String.IsNullOrEmpty(price.Trim()))
                return "Please input the product price";

            int _price;
            if (int.TryParse(price, out _price) == false)
                return "The price not correct format";
            return "";
        }

        private void Btnadd_Click(object sender, EventArgs e)
        {
            string name = txtname.Text.Trim();
            string price = txtPrice.Text.Trim();
            string message = checkValue(name, price);

            int visible = (chkVisible.Checked) ? 1 : 0;
            if (message != "")
            {
                MessageBox.Show(message);
                return;
            }
            if (GproductType == null)
            {
                MessageBox.Show("Please choose the product type to insert");
                return;
            }
            Unit _unit = (Unit)(cmbunit.SelectedItem);
            GproductDb.AddProduct(name, _unit.UnitId, decimal.Parse(txtPrice.Text), GproductType.TypeId, visible);
            LoadProductToList(GproductType);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string name = txtname.Text.Trim();
            string price = txtPrice.Text.Trim();
            string message = checkValue(name, price);
            int visible = (chkVisible.Checked) ? 1 : 0;

            if (message != "")
            {
                MessageBox.Show(message);
                return;
            }
            if (GproductType == null)
            {
                MessageBox.Show("Please choose the product type to insert");
                return;
            }
            if (Gproduct == null)
            {
                MessageBox.Show("Please choose the product to update");
                return;
            }
            Unit _unit = (Unit)(cmbunit.SelectedItem);
            gproductdb.UpdateProduct(Gproduct.ProductId, name, _unit.UnitId, int.Parse(price), GproductType.TypeId, visible);
            LoadProductToList(GproductType);
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            if (Gproduct == null)
            {
                MessageBox.Show("Please the product to delete");
                return;
            }
            GproductDb.DeleteProduct(Gproduct.ProductId);
            LoadProductToList(GproductType);
        }

        private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }




    }
}
